<?php

class Boot_by_player {
	public function __construct() {
	   add_action( 'wp_enqueue_scripts', array(&$this, 'enqueue_scripts'), 20 );
	   add_shortcode( 'boot_by_player', array(&$this, 'test_func'));
	}
	
	public function test_func( $atts ){
	   global $wpdb;
		$output = ob_start();
	   
		echo '<form id="findBootsByPlayer">
				Spillernavn: <input type="search" name="playername">
				<input type="button" value="Søg">
				</form>
				<div id="results">
				</div>';


		$result = $wpdb->get_results('SELECT `ID`, `post_title`, `post_type`, `post_name` FROM `ff_wp_posts` WHERE `post_type` = "players";');
		$count = 0;

		?>
		<script type="text/javascript" >
		var playersBoots = {};
		<?php
		$dir = rtrim(ABSPATH, '/') . "\wp-content\plugins\affiliate-plugin\AffiliatePlugin.php";
		include_once BS_DIR . $dir;
		$af_plugin = new AffiliatePlugin();

		foreach ($result as $player) {

		   $position = wp_get_post_terms($player->ID, 'position');
		   $boot_brands = wp_get_post_terms(get_post_meta($player->ID, 'boot', true), 'brand');
		   
	        echo 'playersBoots['.$count.'] = {};';
	        echo 'playersBoots['.$count.'] = '.json_encode($player).';';
	        
	        echo 'playersBoots['.$count.'].playerThumbnail = "'.wp_get_attachment_url(get_post_thumbnail_id($player->ID)).'";';
	        echo 'playersBoots['.$count.'].bootThumbnail = "'.wp_get_attachment_url(get_post_thumbnail_id(get_post_meta($player->ID, 'boot', true))).'";';
	        echo 'playersBoots['.$count.'].boot = "'.get_the_title(get_field('boot', $player->ID)).'";';
	        echo 'playersBoots['.$count.'].bootBrand = "'.$boot_brands[0]->name.'";';
		  	echo 'playersBoots['.$count.'].bootPageUrl = "'.get_permalink(get_post_meta($player->ID, 'boot', true)).'";';
	        echo 'playersBoots['.$count.'].club = "'.get_the_title(get_field('club', $player->ID)).'";';
	        echo 'playersBoots['.$count.'].position = "'.$position[0]->name.'";';
	        echo 'playersBoots['.$count.'].nation = "'.get_field('nation', $player->ID).'";';
	        echo 'playersBoots['.$count.'].foot = "'.get_field('foot', $player->ID).'";';
	        echo 'playersBoots['.$count.'].age = "'.$this->getAge(get_field('birthday', $player->ID)).'";';
	        echo 'playersBoots['.$count.'].clubHistory = {};';

	        $club_history = explode('<br />', nl2br(get_post_meta($player->ID, 'club_history', true)));
	        if(count(array_filter($club_history))) {
		        foreach ($club_history as $key => $club) {
		       		echo 'playersBoots['.$count.'].clubHistory['.$key.'] = "'.trim($club).'";';
		    	}
	    	}

	    	//$price_tables_array = get_post_meta(get_post_meta($player->ID, 'boot', true), 'price_tables');
			//$price_tables = $price_tables_array[0];
			$price_tables = wp_get_post_terms(get_post_meta($player->ID, 'boot', true), 'pricetable');
	    	$cheapest_price = 99999999999999999999999999;
	    	$cheapest_price_link = "";

	    	foreach($price_tables as $index=>$value) {
	    		if($af_plugin->getBestTablePrice($price_tables[$index]->term_id)->price < $cheapest_price) {
	    			$cheapest_price = $af_plugin->getBestTablePrice($price_tables[$index]->term_id)->price;
	    			$cheapest_price_link = $af_plugin->getBestTablePriceLink($price_tables[$index]->term_id);
	    		}
	    	}

	    	echo 'playersBoots['.$count.'].bestPriceLink = "'. $cheapest_price_link . '";';
	    	//dump(wp_get_post_terms(3684, 'pricetable'));

			echo "\n";						
	    	
	        $count++;

	    }
	    
	    echo '</script>';

	    
	   $output = ob_get_clean();
	   ob_flush();

	   return $output;
	    //dump(get_post_meta(3684, 'price_tables')[0]);
	    //dump($af_plugin->getBestTablePrice(337));
	}

	
	/**
	 * Adds new stylesheets and scripts
	 * 
	 */
	public function enqueue_scripts()
	{
	   wp_enqueue_style( 'dashicons' );
	   wp_enqueue_style( 'bs-main', BS_ASSETS_URL.'css/main.css', array(), '1.0' );
	   
	   wp_enqueue_script( 'bs-main', BS_ASSETS_URL.'js/main.js', array( 'jquery' ), '1.0', true );
	}

	public function getAge($date) { // Y-m-d format
		return intval(substr(date('Ymd') - date('Ymd', strtotime($date)), 0, -4));
	}
}

new Boot_by_player();